package com.dm.library.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.dm.library.R;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  jiaohongyun
 * 修改时间:  2016/6/20
 */
public class LoadingDialog {
    /**
     * 旋转动画的时间
     */
    private static final int ROTATION_ANIMATION_DURATION = 1200;
    /**
     * 动画插值
     */
    private static final Interpolator ANIMATION_INTERPOLATOR = new LinearInterpolator();
    private Dialog d;
    private TextView textView;
    public static LoadingDialog getInstance(Activity activity) {
        LoadingDialog loadingDialog = new LoadingDialog(activity);
        return loadingDialog;
    }
    @SuppressWarnings("deprecation")
    public LoadingDialog(Activity activity) {
        View view = View.inflate(activity, R.layout.loading_dialog, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        textView = (TextView) view.findViewById(R.id.load_text);

        AnimationDrawable ad = (AnimationDrawable) activity.getResources().getDrawable(R.drawable.loading_progress_round);
        imageView.setBackgroundDrawable(ad);
        ad.start();

        d = new Dialog(activity, R.style.loading_dialog);// 加入样式
        d.setCanceledOnTouchOutside(false);
        Window window = d.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void show() {
        if (null != d && !d.isShowing()) {
            d.show();
        }
    }

    public void show(String content) {
        if (null != d && !d.isShowing()) {
            textView.setText(content);
            d.show();
        }
    }

    public void dismiss() {
        if (d != null) {
            d.dismiss();
        }
    }

    public void setCancel() {
        if (d != null) {
            d.setCancelable(false);
        }
    }

    public boolean isShowing() {
        return d.isShowing();
    }

    public void setCancelable(boolean canCancel){
        if(null != d){
            d.setCancelable(canCancel);
        }
    }
}
